<!--  -->
<template>
  <div>
    <div v-if="requestError" class="text-c errorPlace">
      <img width="100px" height="100px" :src="require('@/assets/images/pic_neterror.png')" alt="获取数据失败" />
      <div class="color-b font14" style="margin: 10px 0 0 0">
        获取数据失败!
      </div>
      <van-button plain round  size="small" color="#7232dd" type="primary" @click="getdetail">点击重试</van-button>
    </div>
    
    <van-form v-else @submit="submitFromData">
      <div class="font15 mt10 pl10">
        <span class="titletext ">{{titledata.title}}</span>
        <div class="graytext  mt10 font13">
          <span >工单编号：</span>
          <span class="titletext">{{titledata.pmInsId}}</span>
        </div>
      </div>
      <!-- <van-tabs v-model="active" color="#1989fa" sticky animated line-width="33%" offset-top="50">
        <van-tab title="收入确认信息"> -->
          <newtabone ref="toMsgs" :fromdata='fromdata' :showHistory='showHistory' :contractList='contractList' :billingList='billingList'  :location="params.location" :approveList='approveList' :historydata='historydata' :fromdata3='fromdata3'/>
        <!-- </van-tab>
        <van-tab title="项目交底信息">
          <tabtwo :fromdata='fromdata2' :contractList='contractList' :billingList='billingList' />
        </van-tab>
        <van-tab title="合同交底信息">
          <tabthree :fromdata='fromdata3' :contractList='contractList' :incomeList='incomeList'
            :billingList='billingList' />
        </van-tab>
      </van-tabs> -->

      <div class="left_botton" v-if="showMsgBox">
        <button block type="button" @click="toMsgValue()"
          style="color:#60a9ec; display: inline-block;width:100%;border: 0px solid #60a9ec;border-radius: 15px 0px 0px 15px;
          padding:10px 10px 10px 15px;font-size:13px">意见</button>
      </div>
      <div v-if="showNext==true" class="footer">
        <van-button block type="info" native-type="submit"
          style="display: inline-block;width:100%;border: 0px solid #60a9ec;">流转下一步</van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import { getFormDetail, getSrqrRequest, wfWorkItemModel, getWfOptMags,getWfLinkTracks } from '@/api/dictapi'
import util from "@/assets/js/public";
import tabone from "@/views/srqr/tabone"
import tabtwo from "@/views/srqr/tabtwo"
import tabthree from "@/views/srqr/tabthree"
import { mapGetters } from "vuex"
import { Notify, Dialog, Toast } from 'vant'
import moment from 'moment'
import newtabone from "@/views/srqr/newtabone"

export default {
  data() {
    return {
      active: 0,
      requestError:false,
      params: this.$route.query,
      showHistory: false,
      historydata: {},//已办显示当前办理人
      fromdata: {},//收入确认信息
      fromdata2: {},//项目交底信息
      fromdata3: {},//合同交底信息
      contractList: [],//合同列表
      incomeList: [],//已交底合同信息列表
      billingList: [],//计费科目信息列表
      approveList: [],//审批意见列表
      titledata:{}, //主单据信息
      showNext: false,
    }
  },
  computed: {
    ...mapGetters(['user']),
    ...mapGetters(['userinfo'])
  },
  //生命周期--创建完成
  created() {
    if (this.params.todoType=='2'||this.params.todoType=='4') {
      this.showHistory = true;
      this.showNext = false;
    }else{  
      this.showHistory = false;
      this.showNext = true;
    }
    this.getdetail();
  },
  //生命周期--挂载完成
  components: {
    tabone,
    tabtwo,
    tabthree,
    newtabone
  },
  //方法调用
  methods: {
    toMsgValue(){
       this.$refs.toMsgs.tomsg()
    },
    goBack() {
      this.$router.back(-1) //返回上一页
    },
    onError(result){
      this.requestError=true;
      Toast.clear();
      Notify(result.message);
    },
    getdetail() {
      Toast.loading({
        message: '加载中',
        forbidClick: false
      });

      getFormDetail(Object.assign(this.params, { userName: this.user.userName }))
        .then((result) => {
          if (!result.data) {
            this.onError(result);
            return;
          }
          if (result.data.data) {
            this.titledata = result.data.data
            this.params.pmInsId = result.data.data.pmInsId;
            this.getIncomeAffirmByPmInsId();//收入确认页签信息
          }

        })
    },
    getIncomeAffirmByPmInsId() {
      getSrqrRequest('incomeAffirm$api$getIncomeAffirmByPmInsId',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "pmInsId": this.params.pmInsId,
            "currentUserCode": util.encrypt(this.params.userName),
          }
        }
        ))
        .then((result) => {
          Toast.clear();
          if (!result.data) {
            this.onError(result);
            return;
          }
          if (result.data.data) {
            this.requestError=false;
            this.fromdata = result.data.data;
            this.billingList = this.fromdata.incomeListModel;

            this.params.projectID = this.fromdata.projectEstablishNumber
            this.params.projectId = this.fromdata.projectEstablishNumber
            this.params.projectEstablishNumber = this.fromdata.projectEstablishNumber

            this.getProjectDelNewInformationByProjectNumber();//目前看没啥用
            this.getEsopBusinessToProjectByNumber();//项目页签信息
            this.getPaymentContractBlocAllByProjectId();//合同列表，每个页签都有
            this.getIncomContractListByNumber();
          }
          // if (this.showHistory) {
          //   this.wfWorkItemModel();
          // }
          this.getWfOptMags()
        })
    },
    getProjectDelNewInformationByProjectNumber() {
      getSrqrRequest('projectInformation$api$getProjectDelNewInformationByProjectNumber',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "projectID": this.params.projectID,
            "currentUserCode": util.encrypt(this.params.userName),
          }
        }
        )).then((result) => { 
          if (result.data.data) {
            this.fromdata.descriptionFiles = result.data.data.projectInformationForm.projectDecisionDocument
            this.fromdata.incomeFileList = result.data.data.projectInformationForm.contractFile
            this.fromdata.outcomeFileList = result.data.data.projectInformationForm.newContractFile
          }
        })
    },
    getEsopBusinessToProjectByNumber() {
      getSrqrRequest('esopBusinessToProject$api$getEsopBusinessToProjectByNumber',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "projectEstablishNumber": this.params.projectEstablishNumber,
            "currentUserCode": util.encrypt(this.params.userName),
          }
        }
        )).then((result) => {
          if (result.data.data) {
            this.fromdata2 = result.data.data
          }
        })
    },
    getPaymentContractBlocAllByProjectId() {
      getSrqrRequest('contractWarehouseBloc$api$getPaymentContractBlocAllByProjectId',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "projectId": this.params.projectId,
            "currentUserCode": util.encrypt(this.params.userName),
          }
        }
        )).then((result) => {
          if (result && result.data && result.data.data) {
            this.contractList = result.data.data
          }
        })
    },
    getIncomContractListByNumber() {
      getSrqrRequest('esopBusinessToProjectContract$api$getIncomContractListByNumber',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "projectEstablishNumber": this.params.projectEstablishNumber,
            "currentUserCode": util.encrypt(this.params.userName),
          }
        }
        )).then((result) => {

          if (!result.data) {
            Notify(result.message);
            return;
          }
          if (result.data.data) {
            this.params.contractSerialNumber = result.data.data[0].contractSerialNumber
            this.params.contractPmInsId = result.data.data[0].pmInsId
            this.getContractSendBasicData();//合同页签信息
            this.getContractIncomeListAndEsopModel();//已交底合同信息
          }
        })
    },
    getContractSendBasicData() {
      getSrqrRequest('projectManage$api$getContractSendBasicData',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "contractSerialNumber": this.params.contractSerialNumber,
            "currentUserCode": util.encrypt(this.params.userName),
          },
              body:{
                "contractSerialNumber": this.params.contractSerialNumber,
              }
        }
        )).then((result) => {
          if (result.data.data) {
            this.fromdata3 = result.data.data
            this.fromdata3.PROJECT_NAME = this.fromdata.projectEstablishName
          }
        })
    },
    getContractIncomeListAndEsopModel() {
      getSrqrRequest('projectManage$api$getContractIncomeListAndEsopModel',
        Object.assign(this.params, {
          kv: {
            "source": 'MOBILE',
            "contractSerialNumber": this.params.contractSerialNumber,
            "pmInsId": this.params.contractPmInsId,
            "currentUserCode": util.encrypt(this.params.userName),
          }
        }
        )).then((result) => {
          this.incomeList = result.data.data
        })
    },
    // wfWorkItemModel() {
    //   wfWorkItemModel(Object.assign(this.params, { userName: this.user.userName }))
    //     .then((result) => {
    //       if (!result.data) {
    //         Notify(result.message);
    //         return;
    //       }
    //       if (result.data.data) {
    //         this.historydata = result.data.data
    //       }
    //     })
    // },
    
    getWfOptMags() {
      getWfOptMags(Object.assign(this.params), { userName: this.user.userName })
        .then((result) => {
          if (!result.data) {
            Notify(result.message);
            return;
          }
          this.approveList = result.data.data.reverse();
          if(this.showHistory==true){
            this.getWfLinkTracks()
          }
        })
    },
    getWfLinkTracks(){
      getWfLinkTracks(Object.assign(this.params),{userName:this.user.userName})
        .then((result)=>{
          Toast.clear();
          if(!result.data){
            Notify(result.message);
            return;
          }
          if(result.data.data){
            //数据过滤，并生成新的数组
            this.historydata=result.data.data.filter(item=>item.CURRENTSTATE==10)[0]
            if(this.historydata){
                if(this.historydata.MANAGER){
                  this.showHistory=true;
                }else{
                  this.showHistory=false;
                }
            }else{
              this.showHistory=false;
            }
          }else{
            this.showHistory=false;
          }
        })
    },
    submitFromData() { //提交流转
    if(this.fromdata.projectServiceContent!='纯运维'&&(!this.fromdata.scheduleFileList||this.fromdata.scheduleFileList.length<1)){
        Notify('进度证明材料必须上传附件');  
        return;
      }
      if(this.fromdata.projectServiceContent=='纯运维'&&(!this.fromdata.assetsFile||this.fromdata.assetsFile.length<1)){
        Notify('交维资产信息必须上传附件');
        return;
      }
      if(!this.fromdata.descriptionFiles||this.fromdata.descriptionFiles.length<1){
        Notify('决策文件必须上传附件');  
        return;
      }
      if(!this.fromdata.incomeFileList||this.fromdata.incomeFileList.length<1){
        Notify('收入合同扫描件必须上传附件');  
        return;
      }
      if(!this.fromdata.outcomeFileList||this.fromdata.outcomeFileList.length<1){
        Notify('支出合同扫描件必须上传附件');  
        return;
      }
      this.params.formData=this.fromdata;
      
      var formSendData={"htmlParamMap":{}};
      formSendData.htmlParamMap = this.fromdata;
      formSendData.htmlParamMap.incomeList=this.fromdata.incomeListModel
      formSendData.htmlParamMap.incomeListModel=null
      formSendData.id = this.titledata.id;
      formSendData.pmInsId = this.titledata.pmInsId;
      formSendData.processType = this.params.pmInsType;
      formSendData.workItemId = this.params.workItemId;
      console.log("传给递交页面:", this.params)
      var params1 = {
            kv: {
              currentUserCode: util.encrypt(this.params.userName),
              source: 'MOBILE',
              location: this.params.location,
              processInstId: this.params.processInstId,
              processDefName: this.params.processDefName,
              processType: this.params.pmInsType,
            },
      };
      var params2 = {
            kv: {
              "currentUserCode": util.encrypt(this.params.userName),
              "source": 'MOBILE',
              "location": this.params.location,
              "processInstId": this.params.processInstId,
              'processType': this.params.pmInsType,
              'receiptCode': this.params.processInstId
            },
      };
      var params3 = {
            kv: {
              "source": 'MOBILE',
              "currentUserCode": util.encrypt(this.params.userName),
              "workItemId": this.params.todoType==3?"":this.params.workItemId,
              "processInstId": this.params.processInstId,
              "location": this.params.location,
              "notificationId":this.params.todoType==3?this.params.wfnid:""
            },
      };
      localStorage.setItem('submitPageParam', JSON.stringify({
        getDecisionsUrl:'decisionAndUser$api$getDecisions',
            getDecisionsParams:params1,
            getUserUrl:'decisionAndUser$api$getOrgAndUser',
            getUserParams:params2,
            tosubmintUrl:'applicationFormEnhance$api$startSubmitProcess',
            tosubmintParams:params3,
            formData:formSendData,
            fromMoa:this.params.fromMoa,
            type:this.params.todoType==3?"toRead":"toDo"
          }));
      this.$router.push(
        {
          name: 'srqrsubmit'
        }
      );
      // this.$router.push(
      //   {
      //     name: 'srqrsubmit',
      //     params: this.params
      //   }
      // );
    },

  }
}
</script>
<style  scoped>
.errorPlace{
  position:absolute;
  margin-top: -80px;
  top: 50%;
  margin-left: -50px;
  left: 50%;
}
</style>